import baseUrl from "./baseUrl";
import { back, showModal, msg, loading } from "../utils/wxAPI";
import { HTTP_STATUS } from "./config";
import { getStorage, removeStorage, setGloBalData } from "../utils/storage";
import { getEnv } from "../utils/env";
import { islogin } from "../utils/wxLogin";

export const Ajax = (params) => {
  let contentType = "application/json";
  contentType = params.contentType || contentType;
  return new Promise((reslove, reject) => {
    uni.request({
      url: baseUrl + params.url,
      method: params.method || "GET",
      data: params.data,
      header: {
        token:
          "bearer " +
          (getApp()?.globalData?.token
            ? getApp()?.globalData.token
            : getStorage("token")),
        "content-type": contentType,
        "User-Type": "wap",
        Authorization: "d2FwOlpNOUtuNU50SA==",
      },
      success(res) {
        if (res.statusCode !== HTTP_STATUS.SUCCESS) {
          if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
            showModal({
              t2: "服务端出现了问题",
              btn2text: "重启",
              btn1show: false,
            }).then(() => {
              // #ifdef H5
              location.reload();
              // #endif
              // #ifdef MP-WEIXIN
              uni.switchTab({
                url: "/pages/index/index",
              });
              // #endif
            });
          } else {
            showModal({
              t2: "请求错误码：" + res.statusCode,
              btn1show: false,
            });
          }
          reslove(res);
        } else {
          if (params.url == "/server-user/wap/user/update/parent") {
            reslove(res);
            return false;
          }
          if (res.data.code == HTTP_STATUS.AUTHENTICATE) {
            uni.hideLoading();
            showModal({
              t2: `登录已过期，请重新登录`,
              btn1show: false,
            }).then(async () => {
              // #ifdef H5
              removeStorage("token");
              getApp().globalData.token = "";
              if (getEnv() == "H5") {
                uni.navigateTo({
                  url: "/pages/login/login",
                });
              }
              // #endif

              // #ifdef MP-WEIXIN
              removeStorage("token");
              getApp().globalData.token = "";
              await islogin();
              uni.switchTab({
                url: "/pages/user/user",
              });
              // #endif
            });
            return;
          } else if (res.data.code !== HTTP_STATUS.RUSELT_SUCCESS) {
            let message =
              typeof res.data.msg == "string" ? res.data.msg : "服务器异常";
            uni.hideLoading();
            showModal({
              t2: message,
              btn1show: false,
            }).then(() => {
              reslove(res);
            });
            return;
          }
          reslove(res);
        }
      },
      fail() {
        uni.hideLoading();
        showModal({
          t2: "请检查网络是否连接",
          btn1show: false,
        });
      },
    });
  });
};

export const AjaxuploadFile = (params) => {
  let contentType = "multipart/form-data";
  contentType = params.contentType || contentType;

  return new Promise((reslove, reject) => {
    uni.uploadFile({
      filePath: params.filePath,
      name: "file",
      url: baseUrl + params.url,
      formData: {
        type: "api",
      },
      header: {
        token:
          "bearer " +
          (getApp()?.globalData?.token
            ? getApp()?.globalData.token
            : getStorage("token")),
        "context-type": contentType,
        "User-Type": "wap",
        Authorization: "d2FwOlpNOUtuNU50SA==",
      },
      success(res) {
        if (res.statusCode !== HTTP_STATUS.SUCCESS) {
          if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
            showModal({
              t2: "服务端出现了问题",
              btn2text: "重启",
              btn1show: false,
            }).then(() => {
              // #ifdef H5
              location.reload();
              // #endif
              // #ifdef MP-WEIXIN
              uni.switchTab({
                url: "/pages/index/index",
              });
              // #endif
            });
          } else {
            showModal({
              t2: "请求错误码：" + res.statusCode + "服务器出错",
              btn1show: false,
            });
          }
          reslove(res);
        } else {
          let info = JSON.parse(res.data);
          if (info.code == HTTP_STATUS.AUTHENTICATE) {
            showModal({
              t2: "登录已过期,请重新登录",
              btn1show: false,
            }).then(() => {
              // #ifdef H5
              removeStorage("token");
              getApp().globalData.token = "";
              if (getEnv() == "H5") {
                uni.navigateTo({
                  url: "/pages/login/login",
                });
              }
              // #endif
            });
          } else if (info.code !== HTTP_STATUS.RUSELT_SUCCESS) {
            let message = typeof info.msg == "string" ? info.msg : "服务器异常";
            showModal({
              t2: message,
              btn1show: false,
            });
          }
          reslove(info);
        }
      },
      fail() {
        uni.hideLoading();
        showModal({
          t2: "请检查网络是否连接",
          btn1show: false,
        });
      },
    });
  });
};
